<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3> 双端队列实现 double ended queue</h3><br/>
    <h3> 双端队列应用：回文</h3>
    <script>
        class Deque{
            constructor(){
                this.item=[];

            }
            //前端插入addFront()
            addFront(element){
                this.item.unshift(element);
            }
            //后端插入addBack()
            addBack(element){
                this.item.push(element);
            }
            //前端移除removeFront()
            removeFront(){
                return this.item.shift();
            }
            //后端移除removeBack()
            removeBack(){
                return this.item.pop();
            }
            //peekFront()返回前端第一个元素
            peekFront(){
                if(this.item.length ===0){
                    return undefined;
                }
                else{
                    return this.item[0];
                }
            }
             //peekBack()返回后端第一个元素
             peekFront(){
                if(this.size() ===0){
                    return undefined;
                }
                else{
                    return this.item[this.size()-1];
                }
            }
            //isEmpty()
            isEmpty(){
                return this.size() === 0;

            }
            //size()
            size(){
                return this.item.length;
            }
            //toString()
            toString(){
                return this.item.toString();
            }
        
        }

        //test
   /*      const deque = new Deque();
        deque.addBack("john");
        deque.addBack("jack");
        deque.addFront("camila");
        console.log(deque.toString());
        console.log(deque.removeBack()); */

        function palindromeChecker(aString){
            if(aString === undefined || aString === null||
            (aString!=null&&aString.length===0))
            {
                return false;
            }
            const deque = new Deque();
            const lowerString = aString.toLocaleLowerCase().split(" ").join("");
            let isEqual = true;
            let firstChar,lastChar;
            for(let i = 0;i<lowerString.length;i++)
            {
                deque.addBack(lowerString.charAt(i));
            }

            while(deque.size()>1&&isEqual)
            {
                firstChar = deque.removeFront();
                lastChar = deque.removeBack();
                if(firstChar!=lastChar)
                {
                    isEqual = false;
                }
            }

            return isEqual;
            

           
            
        }
        let strs = ["a","aba","ade","hello","this is a test","aka aka"];
            let str;
            for(let i =0;i<strs.length;i++){
                str = strs[i];
                console.log(str,palindromeChecker(str)); 
            }




        
    </script>
</body>
</html>